* WINDOWS PREAMBLE. 
#delimit ;
global rawDir "LOCATION OF RAW CPS DATA";
global dataDir "WORKING DIRECTORY";
global currDir "PROGRAM DIRECTORY";
cd "$currDir" ;
capture log close ;
clear all ;
log using 1A_housing_merge.log, replace text;
cd "$dataDir" ;
set more off ;

***************************************************************************;

* FOR SOME REASON FAMILY HOUSING SUBSIDY IS NOT INCLUDED IN THE IPUMS CPS. ;

***************************************************************************;

***************************************************************************;
* START WITH THE IPUMS CPS IDENTIFIERS ;
***************************************************************************;

* READ IN RAW IPUMS WITH IDENTIFIERS ;
local cps "cps_00007";
capture erase `cps'.do ;
capture erase `cps'.dat ;
capture erase `cps'.dat.gz ;
local from "$rawDir\\`cps'.dat.gz" ;
local to "$dataDir\\`cps'.dat.gz"  ;
copy `from' `to' , replace;
shell 7za e `cps'.dat.gz ;
local from "$rawDir\\`cps'.do" ;
local to "$dataDir\\`cps'.do"  ;
copy `from' `to' , replace;
quietly do `cps'.do ;
summ ;
save cps_ipums_id_raw, replace ;
clear ;

***************************************************************************;
* PREP RAW CPS FOR MERGE ;
***************************************************************************;
local cps "cps_extract_092816_stata13";
capture erase `cps'.dta ;
capture erase `cps'.zip ;
local from "$rawDir\\`cps'.zip" ;
local to "$dataDir\\`cps'.zip"  ;
copy `from' `to' , replace;
shell 7za e `cps'.zip ;

use cps_extract_092816_stata13 ;
keep year h_seq p_pos f_houssub f_mv_sl;
drop if year == 1988 & p_pos <= 40 ;
gen pernum = p_pos ;
replace pernum = pernum-40 if pernum>40 ;
rename h_seq hseq ;
drop p_pos ;
sort year hseq pernum ;
save rawcpsmerge, replace ;
clear ;

use cps_ipums_id_raw ;
keep if year >= 1985 ;
keep year hseq pernum serial ;
sort year hseq pernum ;
merge 1:1 year hseq pernum using rawcpsmerge ;
rm rawcpsmerge.dta ;

keep year serial pernum f_houssub f_mv_sl ;
sort year serial pernum ;
duplicates report year serial pernum ;
save housing_sub_cps, replace ;

exit ;
log close ;

